<?php
declare (strict_types = 1);

namespace app\service\logic\common\order;

use app\constant\LogicConstant;
use app\model\communal\OrderConsultModel;
use app\model\communal\OrderConsultRefundLogModel;
use app\model\communal\OrderConsultRefundModel;
use app\service\logic\BaseService;
use app\service\logic\common\user\CoinService;
use Carbon\Carbon;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\Model;

final class OrderConsultRefundService extends BaseService
{
    protected string $modelClassName = 'OrderConsultRefundModel';

    /**
     * @param $params
     */
    public function wechatRefundCallback($params)
    {
        event('OrderConsultRefundSuccess', $params);
    }

    /**
     * @param $params
     */
    public function refundSuccess($params)
    {
        $order = OrderConsultModel::where('number_md5', $params['out_trade_no'])->findOrEmpty();
        $orderRefund = OrderConsultRefundModel::where('out_request_no', $params['out_refund_no'])->findOrEmpty();
        if (
            !$order->isEmpty() && 5 != $order['refund_status'] && !$orderRefund->isEmpty()
        ) {
            // 变更退款状态
            $order->save(['refund_status' => 5]);
            // 日志
            $insertLogs = [[
                'operator' => '微信支付',
                'order_consult_id' => $order['id'],
                'order_consult_refund_id' => $orderRefund['id'],
                'log_type' => OrderConsultRefundLogModel::LOG_TYPE_ONE,
                'log_json' => [
                    'relation_data' => [[
                        'data_type' => LogicConstant::LOG_DATA_TYPE_ONE,
                        'data_logic_type' => LogicConstant::LOG_DATA_LOGIC_TYPE_ZERO,
                        'data' => [
                            'key' => '',
                            'value' => '您的退款金额已在 '. Carbon::now()->format('Y-m-d H:i:s') .' 入账，入账金额为'.$orderRefund['payment_amount'].'元'
                        ]
                    ]],
                ],
            ]];
            event('OrderConsultRefundLog', $insertLogs);
            // 退元心币
            invoke(CoinService::class)->back([
                'user_id' => $order['user_id'],
                'coin_count' => $order['deduct_coin'],
                'type' => 4,
            ]);
        }
    }

    /**
     * 详细情况...
     *
     * @param $OrderConsultId
     * @param array $with
     * @param null $eachCall
     * @return OrderConsultRefundModel|array|false|mixed|Model
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     */
    public function readByOrderConsultId($OrderConsultId,  $with = [], $eachCall = null)
    {
        $model = OrderConsultRefundModel::field(OrderConsultRefundModel::$column)
            ->with($with)
            ->where('order_consult_id', $OrderConsultId)
            ->findOrFail();
        if (is_callable($eachCall)) {
            $model = call_user_func($eachCall, $model);
        }
        return $model;
    }
}
